<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Like
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 * @version    $Id: Controller.php 2010-09-07 16:05 idris $
 * @author     Idris
 */

/**
 * @category   Application_Extensions
 * @package    Like
 * @copyright  Copyright Hire-Experts LLC
 * @license    http://www.hire-experts.com
 */

class Like_Widget_ProfileLikesController extends Engine_Content_Widget_Abstract
{
	protected $_childCount;
	
  public function indexAction()
  {
  	if (!Engine_Api::_()->getDbTable('modules', 'core')->isModuleEnabled('like')){
  		$this->setNoRender();
  		return ;
  	}
  	         
    if (!Engine_Api::_()->getApi('settings', 'core')->getSetting('like.profile.enabled', 1)){
      $this->setNoRender();
      return ;
    }

    if (!Engine_Api::_()->user()->getViewer()->getIdentity()){
      $this->setNoRender();
      return ;
    }

  	$this->view->subject = $subject = Engine_Api::_()->core()->getSubject();
    $likeTable = Engine_Api::_()->getDbTable('likes', 'like');
    
    if ($subject->getType() != 'user'){
      $this->setNoRender();
      return ;
    }

    $settings = Engine_Api::_()->getApi('settings', 'core');
    $ipp = $settings->getSetting('like.profile.count', 9);
    
    $params = array('like_id' => $subject->getIdentity(), 'limit' => $ipp);
    $this->view->items = Engine_Api::_()->like()->getLikedItems($params);
    
    shuffle($this->view->items);
    
    $this->view->total = $total = $likeTable->getLikedCount(array('user_id' => $subject->getIdentity()));
    
    if( $this->_getParam('titleCount', false) && $total > 0 ){
      $this->_childCount = $total;
    }
    $this->view->ipp = $ipp;
    
    if (!$total || !count($this->view->items)){
      $this->setNoRender();
      return ;
    }

  }
  
  public function getChildCount()
  {
    return $this->_childCount;
  }
}